package com.example.waitersoriginal;



	import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;








import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
	public class LoginActivity extends Activity {
		
		// Progress Dialog
		private ProgressDialog pDialog;

		// Creating JSON Parser object
		JSONParser jParser = new JSONParser();

			// url to get all products list
		private static String url_login_funcionario = "http://192.168.1.7/waiters/login_funcionario.php";

		// JSON Node names
		private static final String TAG_SUCCESS = "success";
		private static final String TAG_CODFUNCIONARIO = "codFuncionario";
		private static final String TAG_NMSENHA = "nmSenha";

		// products JSONArray
		EditText inputCodFuncionario;
		EditText inputNmSenha;
		
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			requestWindowFeature(Window.FEATURE_NO_TITLE);
	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
	                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
			super.onCreate(savedInstanceState);
			setContentView(R.layout.login_funcionario);
			
			// Edit Text
			inputCodFuncionario = (EditText) findViewById(R.id.codFuncionarios);
			inputNmSenha = (EditText) findViewById(R.id.nmSenha);
			
			Button btnDesbloquear = (Button) findViewById(R.id.btnDesbloquear);
			
			
			new LogarFuncionario().execute();
		
		
			
			btnDesbloquear.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// creating new product in background thread
				new LogarFuncionario().execute();
			}
		});
		
		
		
	}
	

	/**
	 * Background Async Task to Load all product by making HTTP Request
	 * */
	class LogarFuncionario extends AsyncTask<String, String, String> {			/**
			 * Before starting background thread Show Progress Dialog
			 * */
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				pDialog = new ProgressDialog(LoginActivity.this);
				pDialog.setMessage("Teste...");
				pDialog.setIndeterminate(false);
				pDialog.setCancelable(true);
				pDialog.show();
			}

			/**
			 * Deleting product
			 * */
			protected String doInBackground(String... args) {
				
				String codFuncionario = inputCodFuncionario.getText().toString();
				String nmSenha = inputNmSenha.getText().toString();
				
				// Check for success tag
				int success;
				try {
					// Building Parameters
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair("codFuncionario", codFuncionario));
					params.add(new BasicNameValuePair("nmSenha", nmSenha));

					// getting product details by making HTTP request
					JSONObject json = jParser.makeHttpRequest(
							url_login_funcionario, "GET", params);

					// check your log for json response
					Log.d("Login funcionario", json.toString());
					
					// json success tag
					success = json.getInt(TAG_SUCCESS);
					if (success == 1) {
						Intent i = new Intent(getApplicationContext(), MainActivity.class);
						startActivity(i);
						finish();
					} else {
						
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

				return null;
			}
		
			/**
			 * After completing background task Dismiss the progress dialog
			 * **/
			protected void onPostExecute(String file_url) {
				// dismiss the dialog once product deleted
				pDialog.dismiss();

			}

		}
}
